﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using DataAccess;
using System.Web.Security;
public partial class admin_Create_Report : System.Web.UI.Page
{
    //Create new decryption object
    Decryption objdec = new Decryption();
    // String clientid = "1000";
    // int index = 0;
    string subject="";
    string body_content="";
    protected void Page_Load(object sender, EventArgs e)
    {
        // CreateConn cc = new CreateConn();
        if (!IsPostBack)
        {
            string report_type = Request.QueryString["report_type"].ToString();
            if (report_type.Equals("VA"))
            {
                Session["subject"] = "New Vibration Report Available";
            }
            else
            {
                Session["subject"] = "New Oil Report Available";
  

                NewVAlink.InnerText = "Create New Oil Report";
                NewVAlink.HRef = "../admin/Mng_OilReport.aspx";
            }
            string clientname = (String)Session["clientName"];
            string clientid = (String)Session["clientid"];
            clientname_lb.Text = clientname;
            EmailRceivers_Dropdownlist.Items.Clear();
         
            string query = "SELECT user_id,username,u.email FROM Users u JOIN Clients c on c.client_id= u.client_id Where c.client_id='" + clientid + "'";
            CreateConn cc = new CreateConn();
            DataTable dt = cc.SQLConnDataTable(query);
            ListItem lt = new ListItem();
            lt.Text = "Select all";
            lt.Value = "0";
            EmailRceivers_Dropdownlist.Items.Add(lt);
            foreach (DataRow dr in dt.Rows)
            {
                ListItem lt2 = new ListItem();
                lt2.Text = dr["username"].ToString();
                lt2.Value = dr["email"].ToString();
                EmailRceivers_Dropdownlist.Items.Add(lt2);
            }


        }
    }

  
   
      


   

    #region dyanamic checkboxes
    //Function to generate the dyanamic checkboxes
    private void AddCheckboxes(string strCheckboxText)
    {
        CheckBox chkList1;
        try
        {

            for (int intControlIndex = 0; intControlIndex < 5; intControlIndex++)
            {
                chkList1 = new CheckBox();
                chkList1.Text = strCheckboxText;
                chkList1.ID = "Chk" + intControlIndex;
                chkList1.Font.Name = "Verdana";
                chkList1.Font.Size = 9;
                chkList1.Visible = true;

            }
        }
        catch (Exception )
        {
            // throw new Exception(exp.Message);
        }
    }
    #endregion
  

    protected void submit_button_Click(object sender, EventArgs e)
    {

        string email = EmailRceivers_Dropdownlist.SelectedValue;
        string subject = Session["subject"].ToString();
        string strMsg = "Please Login in www.i2iportal.com.au to collect your report!";
        if (email != "0")
        {
            Email objmail = new Email();
            if (!objmail.SendEmail(email, subject, strMsg))
            {
            }
            else
            {
                Response.Write("<script language=javascript> alert('Email Sent!')</script>");

            }
        }
        else
        {
            int count = EmailRceivers_Dropdownlist.Items.Count;
            for (int i=1; i < count+1;i++ )
            {
                Email objmail = new Email();
               System.Collections.IEnumerator  ien;
               ien =  EmailRceivers_Dropdownlist.Items.GetEnumerator();
                while(ien.MoveNext()){
                    ListItem useritem = (ListItem)ien.Current;
                    email = useritem.Value;
               objmail.SendEmail(email,subject,strMsg);
                }
            }
            Response.Write("<script language=javascript> alert('Email Sent!')</script>");

        }
       


    }
    protected void NewOilButton_Click(object sender, EventArgs e)
    {
       
        Response.Redirect("Mng_OilReport.aspx");
        //Response.Write("<script language=javascript>window.close</script>");
      
    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {


        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }
 
}
